﻿using NiuX;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCsvParser;
using TinyCsvParser.Mapping;

namespace NiuX.Utils.Csv.Utils;

public static class TinyCsvUtility
{
    public static CsvParserOptions DefaultCsvParserOptions { get; set; } = new(true, ',');

    public static Encoding DefaultEncoding { get; set; } = Encoding.UTF8;


    /// <summary>
    /// 读取并转换成 List
    /// </summary>
    /// <remarks>https://www.thecodebuzz.com/read-csv-file-in-net-core/</remarks>
    public static List<T> ReadAsList<T>(string filePath, CsvMapping<T> mapping) where T : class, new() =>
        new CsvParser<T>(DefaultCsvParserOptions, mapping).ReadFromFile(filePath, DefaultEncoding).Select(x => x.Result).ToList();

}